var url = require("url");

module.exports.getFullUrl = function (cUrl, rUrl) {
    if (rUrl.indexOf('http') == 0) return rUrl;

    var ctx = url.parse(cUrl);
    var cPath = ctx.pathname;

    if (rUrl.indexOf('/') == 0) {
        cPath = rUrl;
    } else {
        // a/b/c.html --- x.html
        if (cPath.length > 1 && /\/$/.test(cPath)) { // 以/结尾
            cPath = cPath.substring(0, cPath.length - 2);
        }
        // console.log(ctx.pathname);
        var lastSlash = cPath.lastIndexOf("/");
        cPath = cPath.substring(0, lastSlash + 1) + rUrl;
    }
    ctx.pathname = cPath;
    return url.format(ctx);
};

var re = /href\s*?=\s*?"([^"]+?)"/g;
module.exports.getHrefInHtml = function (html) {
    var list = html.match(re);
    return list;
};

//
var re2 = /href\s*?=\s*?"([^"]+?)"/;
module.exports.urlFilter = function (list, blacklist) {
    // console.log('urlFilter', blacklist);
    if (!list) {
        return null;
    }

    var nList = list.map(function (item) {
        return item.match(re2)[1]; // href='xxx' => xxx
    });

    nList = nList.filter(function (item) {
        var valid = true;
        var index = 0;
        while (valid && index < blacklist.length) {
            var regex = new RegExp(blacklist[index]);
            if (regex.test(item)) {
                valid = false;
            }
            index++;
        }
        if (valid) {
            // console.log('valid', item);
        } else {
            // console.log('not valid', item);
        }
        return valid;
    });
    return nList;
};

var exec = require('child_process').exec;
var spawn = require('child_process').spawn;

module.exports.cmdCall = function (cmd) {
    console.log('cmd call:' + cmd);
    return new Promise(function (fullful, reject) {
        //exec(cmd, function (err, stdout, stderr) {
        spawn(cmd, function (err, stdout, stderr) {
            if (err) throw err;
            if (stderr) reject(stderr);
            fullful('ok---'+stdout);
        });
    });
};